package com.mk.settlement.controller;

import java.util.Date;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.mk.settlement.service.SettlementRelationService;
import com.mk.settlement.model.SettlementOrder;
import com.mk.settlement.model.SettlementRelation;
import com.mk.settlement.model.SettlementResult;
import com.mk.settlement.model.SettlementRule;
import com.mk.settlement.model.SettlementTempResult;
import com.mk.settlement.service.SettlementOrderService;
import com.mk.settlement.service.SettlementResultService;
import com.mk.settlement.service.SettlementRuleService;
import com.mk.settlement.service.SettlementTempResultService;

/**
 * 结算中心后端管理API.
 * 
 * @author chuaiqing.
 *
 */
@Controller
@RequestMapping(value="/sc/admin", method=RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public class AdminController {
    /**
     * 日志对象
     */
    private Logger log = LoggerFactory.getLogger(AdminController.class);
    
    /**
     * 注入SettlementResultService
     */
    @Autowired
    private SettlementResultService settlementResultService;
    
    /**
     * 注入SettlementOrderService
     */
    @Autowired
    private SettlementOrderService settlementOrderService;
    
    /**
     * 注入SettlementRuleService
     */
    @Autowired
    private SettlementRuleService settlementRuleService;
    
    /**
     * 注入SettlementRelationService
     */
    @Autowired
    private SettlementRelationService settlementRelationService;
    
    /**
     * 注入SettlementTempResultService
     */
    @Autowired
    private SettlementTempResultService settlementTempResultService;
    
    /**
     * 更新结算结果数据.
     * @param settlementResult
     * @return
     */
    @RequestMapping(value="/settlementresult/update")
    @ResponseBody
    public Object upateSettlementResult(SettlementResult settlementResult) {
        Map<String, Object> result = Maps.newHashMap();
        log.info("api: /settlementresult/update");
        log.info("api params is: {}", JSON.toJSONString(settlementResult));
        try {
            settlementResult.setCreateTime(null);
            settlementResult.setUpdateTime(new Date());
            int updateRows = settlementResultService.updateByPrimaryKeySelective(settlementResult);
            result.put("operator", "update");
            result.put("status", "ok");
            result.put("rows", updateRows);
        } catch (Exception e) {
            result.put("operator", "update");
            result.put("status", "error");
            result.put("rows", 0);
            result.put("msg", e.getLocalizedMessage());
        }
        return result;
    }
    
    /**
     * 删除结算结果.
     * @param id
     * 参数：结算结果数据id.
     * @return
     */
    @RequestMapping(value="/settlementsult/delete/{id}")
    @ResponseBody
    public Object deleteSettlementResult(@PathVariable Long id) {
        Map<String, Object> result = Maps.newHashMap();
        log.info("api: /settlementresult/delete/{}", id);
        try {
            int deleteRows = settlementResultService.deleteByPrimaryKey(id);
            result.put("operator", "delete");
            result.put("status", "ok");
            result.put("rows", deleteRows);
        } catch (Exception e) {
            result.put("operator", "delete");
            result.put("status", "error");
            result.put("rows", 0);
            result.put("msg", e.getLocalizedMessage());
        }
        return result;
    }
    
    /**
     * 更新结算基础订单数据.
     * @param settlementOrder
     * @return 
     */
    @RequestMapping(value="/settlementorder/update")
    @ResponseBody
    public Object updateSettlementOrder(SettlementOrder settlementOrder) {
        Map<String, Object> result = Maps.newHashMap();
        log.info("api: /settlementorder/update");
        log.info("api params is: {}", JSON.toJSONString(settlementOrder));
        try {
            settlementOrder.setUpdateTime(new Date());
            int updateRows = settlementOrderService.updateByPrimaryKeySelective(settlementOrder);
            result.put("operator", "update");
            result.put("status", "ok");
            result.put("rows", updateRows);
        } catch (Exception e) {
            result.put("operator", "update");
            result.put("status", "error");
            result.put("rows", 0);
            result.put("msg", e.getLocalizedMessage());
        }
        return result;
    }
    
    /**
     * 删除结算基础订单.
     * @param id
     * 参数：结算基础订单数据id.
     * @return
     */
    @RequestMapping(value="/settlementorder/delete/{id}")
    @ResponseBody
    public Object deleteSettlementOrder(@PathVariable Long id) {
        Map<String, Object> result = Maps.newHashMap();
        log.info("api: /settlementorder/delete/{}", id);
        try {
            int deleteRows = settlementOrderService.deleteByPrimaryKey(id);
            result.put("operator", "delete");
            result.put("status", "ok");
            result.put("rows", deleteRows);
        } catch (Exception e) {
            result.put("operator", "delete");
            result.put("status", "error");
            result.put("rows", 0);
            result.put("msg", e.getLocalizedMessage());
        }
        return result;
    }
    
    /**
     * 更新结算规则API.
     * @param settlementRule
     * @return
     */
    @RequestMapping(value="/settlementrule/update")
    @ResponseBody
    public Object updateSettlementRule(SettlementRule settlementRule) {
        Map<String, Object> result = Maps.newHashMap();
        try {
            int updateRows = settlementRuleService.updateByPrimaryKeySelective(settlementRule);
            result.put("operator", "update");
            result.put("status", "ok");
            result.put("rows", updateRows);
        } catch (Exception e) {
            result.put("operator", "update");
            result.put("status", "error");
            result.put("rows", 0);
            result.put("msg", e.getLocalizedMessage());
        }
        return result;
    }
    
    /**
     * 删除结算规则数据.
     * @param id
     * 参数：结算规则数据id.
     * @return
     */
    @RequestMapping(value="/settlementrule/delete/{id}")
    @ResponseBody
    public Object deleteSettlementRule(@PathVariable Integer id) {
        Map<String, Object> result = Maps.newHashMap();
        try {
            int deleteRows = settlementRuleService.deleteByPrimaryKey(id);
            result.put("operator", "delete");
            result.put("status", "ok");
            result.put("rows", deleteRows);
        } catch (Exception e) {
            result.put("operator", "delete");
            result.put("status", "error");
            result.put("rows", 0);
            result.put("msg", e.getLocalizedMessage());
        }
        return result;
    }
    
    /**
     * 更新结算关系API.
     * @param settlementRelation
     * @return
     */
    @RequestMapping(value="/settlementrelation/update")
    @ResponseBody
    public Object updateSettlementRelation(SettlementRelation settlementRelation) {
        Map<String, Object> result = Maps.newHashMap();
        try {
            int updateRows = settlementRelationService.updateByPrimaryKeySelective(settlementRelation);
            result.put("operator", "update");
            result.put("status", "ok");
            result.put("rows", updateRows);
        } catch (Exception e) {
            result.put("operator", "update");
            result.put("status", "error");
            result.put("rows", 0);
            result.put("msg", e.getLocalizedMessage());
        }
        return result;
    }
    
    /**
     * 删除结算关系API.
     * @param id
     * 参数：结算规则数据id.
     * @return
     */
    @RequestMapping(value="/settlementrelation/delete/{id}")
    @ResponseBody
    public Object deleteSettlementRelation(@PathVariable Long id) {
        Map<String, Object> result = Maps.newHashMap();
        try {
            int deleteRows = settlementRelationService.deleteByPrimaryKey(id);
            result.put("operator", "delete");
            result.put("status", "ok");
            result.put("rows", deleteRows);
        } catch (Exception e) {
            result.put("operator", "delete");
            result.put("status", "error");
            result.put("rows", 0);
            result.put("msg", e.getLocalizedMessage());
        }
        return result;
    }
    
    /**
     * 更新阈值结算数据API.
     * @param settlementTempResult
     * @return
     */
    @RequestMapping(value="/settlementtempresult/update")
    @ResponseBody
    public Object updateSettlementTempResult(SettlementTempResult settlementTempResult) {
        Map<String, Object> result = Maps.newHashMap();
        try {
            int updateRows = settlementTempResultService.updateByPrimaryKeySelective(settlementTempResult);
            result.put("operator", "update");
            result.put("status", "ok");
            result.put("rows", updateRows);
        } catch (Exception e) {
            result.put("operator", "update");
            result.put("status", "error");
            result.put("rows", 0);
            result.put("msg", e.getLocalizedMessage());
        }
        return result;
    }
    
    /**
     * 删除阈值结算数据API.
     * @param id
     * 参数：阈值结算数据id.
     * @return
     */
    @RequestMapping(value="/settlementtempresult/delete/{id}")
    @ResponseBody
    public Object deleteSettlementTempResult(Long id) {
        Map<String, Object> result = Maps.newHashMap();
        try {
            int deleteRows = settlementTempResultService.deleteByPrimaryKey(id);
            result.put("operator", "delete");
            result.put("status", "ok");
            result.put("rows", deleteRows);
        } catch (Exception e) {
            result.put("operator", "delete");
            result.put("status", "error");
            result.put("rows", 0);
            result.put("msg", e.getLocalizedMessage());
        }
        return result;
    }
}
